package com.zx.tradeclient.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.zx.base.lazylist.ImageLoader;
import com.zx.base.util.URLHelper;
import com.zx.tradeclient.R;
import com.zx.tradeclient.entity.ProductItemInfo;

public class ProductPageAdapter extends BaseAdapter {

	private ArrayList<ProductItemInfo> list = new ArrayList<ProductItemInfo>();
	private ImageLoader imageLoader;
	private LayoutInflater inflater;
	private ViewHolder holder = null;
	
	public ProductPageAdapter(Context context, ArrayList<ProductItemInfo> img,
			ImageLoader imageLoader) {
		list = img;
		this.imageLoader = imageLoader;
		this.inflater=LayoutInflater.from(context);
	}

	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		if (convertView == null) {
			convertView=inflater.inflate(R.layout.product_page_list_item, null);
			holder=new ViewHolder();
			holder.page_icon =(ImageView)convertView.findViewById(R.id.product_page_list_item_icon);
			holder.page_name=(TextView)convertView.findViewById(R.id.product_page_list_item_name);
			holder.page_price=(TextView)convertView.findViewById(R.id.product_page_list_item_price);
			holder.page_categ=(TextView)convertView.findViewById(R.id.product_page_list_item_categ);
			holder.page_comname=(TextView)convertView.findViewById(R.id.product_page_list_item_comname);
			holder.page_area=(TextView)convertView.findViewById(R.id.product_page_list_item_area);
			convertView.setTag(holder);
		}else{
			holder=(ViewHolder)convertView.getTag();
		}
		
		
		ProductItemInfo info=list.get(position);
		imageLoader.DisplayImage(URLHelper.mPrefixImg+info.getPage_icon(), holder.page_icon, R.drawable.page_icon_default);
		holder.page_icon.setScaleType(ScaleType.CENTER_CROP);
		holder.page_name.setText(info.getPage_name());
		holder.page_price.setText(info.getPage_price());
		holder.page_categ.setText(info.getPage_categ());
		if(info.getPage_comname().length() > 10){
			holder.page_comname.setText(info.getPage_comname().substring(0, 9) + "...");
		}else{
			holder.page_comname.setText(info.getPage_comname());
		}
		
		if(info.getPage_area().length() > 5){
			holder.page_area.setText(info.getPage_area().substring(0, 3) + "...");
		}else{
			holder.page_area.setText(info.getPage_area());
		}
		
		
		return convertView;
	}
	
	static class ViewHolder{
		public ImageView page_icon;
		public TextView page_name;
		public TextView page_price;
		public TextView page_categ;
		public TextView page_comname;
		public TextView page_area;
	}

}